# Maintainer: Alexey Pavlov <alexpux@gmail.com>

_realname=mesa
pkgbase=mingw-w64-${_realname}
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
pkgver=18.0.2
pkgrel=1
pkgdesc="Open-source implementation of the OpenGL specification (mingw-w64)"
arch=('any')
depends=("")
makedepends=("${MINGW_PACKAGE_PREFIX}-gcc"
             "${MINGW_PACKAGE_PREFIX}-clang"
             "scons"
             "python3-mako")
url="https://www.mesa3d.org/"
license=('MIT')
options=('staticlibs' 'strip')
source=(https://mesa.freedesktop.org/archive/${_realname}-${pkgver}.tar.xz{,.sig}
        001-extra-libs.patch
        link-ole32.patch
        osmesa.pc)
sha256sums=('98fa159768482dc568b9f8bf0f36c7acb823fa47428ffd650b40784f16b9e7b3'
            'SKIP'
            'bc9bb5013ac80ded47ad164ae1ef58cc9a39784eb4bf61e8c7d654bb273b05a9'
            'a80e9f2084933ce2bf4fea7b9a24401bf5eeac6fb33dd146588f25a77be59ead'
            'fdf26548336cc7e5700560c6636a87ffa63daa3048fa94cf4a4a0b50890c9327')
validpgpkeys=('8703B6700E7EE06D7A39B8D6EDAE37B02CEB490D')
validpgpkeys+=('946D09B5E4C9845E63075FF1D961C596A7203456') # Andres Gomez <tanty@igalia.com>
validpgpkeys+=('E3E8F480C52ADD73B278EE78E1ECBE07D7D70895') # Juan Antonio Suárez Romero (Igalia, S.L.) <jasuarez@igalia.com>"

case ${MINGW_CHOST} in
  i686*)
    _mach=x86
  ;;
  x86_64*)
    _mach=x86_64
  ;;
esac

# Helper macros to help make tasks easier #
apply_patch_with_msg() {
  for _patch in "$@"
  do
    msg2 "Applying $_patch"
    patch -Nbp1 -i "${srcdir}/$_patch"
  done
}

del_file_exists() {
  for _fname in "$@"
  do
    if [ -f $_fname ]; then
      rm -rf $_fname
    fi
  done
}
# =========================================== #

prepare() {
  cd ${srcdir}/${_realname}-${pkgver}
# Disable this patch.  llvm-config does not return the proper
# values and we probably don't need to call it anyway.

#  patch -p1 -i ${srcdir}/001-extra-libs.patch
  apply_patch_with_msg link-ole32.patch
}

build() {
  cd ${srcdir}/${_realname}-${pkgver}
  LLVM=${MINGW_PREFIX} \
  CFLAGS="-march=core2 -pipe -D_USE_MATH_DEFINES" \
  CXXFLAGS="-march=core2 -pipe -std=c++11 -D_USE_MATH_DEFINES" \
  LDFLAGS="-static -s" \
  PROCESSOR_ARCHITECTURE="${CARCH}" \
  scons build=release platform=windows toolchain=mingw machine=${_mach}
}

package() {
  cd ${srcdir}/${_realname}-${pkgver}
  mkdir -p ${pkgdir}${MINGW_PREFIX}/bin
  mkdir -p ${pkgdir}${MINGW_PREFIX}/lib/pkgconfig
  mkdir -p ${pkgdir}${MINGW_PREFIX}/include/mesa/GL

  cp -f ${srcdir}/${_realname}-${pkgver}/build/windows-${_mach}/gallium/targets/libgl-gdi/opengl32.dll ${pkgdir}${MINGW_PREFIX}/bin/
  cp -f ${srcdir}/${_realname}-${pkgver}/build/windows-${_mach}/gallium/targets/graw-gdi/graw.dll ${pkgdir}${MINGW_PREFIX}/bin/
  cp -f ${srcdir}/${_realname}-${pkgver}/build/windows-${_mach}/gallium/targets/graw-gdi/libgraw.a ${pkgdir}${MINGW_PREFIX}/lib/
  cp -f ${srcdir}/${_realname}-${pkgver}/build/windows-${_mach}/gallium/targets/osmesa/osmesa.dll ${pkgdir}${MINGW_PREFIX}/bin/
  cp -f ${srcdir}/${_realname}-${pkgver}/build/windows-${_mach}/gallium/targets/osmesa/libosmesa.a ${pkgdir}${MINGW_PREFIX}/lib/

  cp -f ${srcdir}/${_realname}-${pkgver}/include/GL/gl.h ${pkgdir}${MINGW_PREFIX}/include/mesa/GL/
  cp -f ${srcdir}/${_realname}-${pkgver}/include/GL/osmesa.h ${pkgdir}${MINGW_PREFIX}/include/mesa/GL/
  cp -f ${srcdir}/osmesa.pc ${pkgdir}${MINGW_PREFIX}/lib/pkgconfig/
  # fixup our .pc file with valid info
  sed -e "s|/mingw64|${MINGW_PREFIX}|g" \
    -e "s|8|${pkgver}|g" \
    -i ${pkgdir}${MINGW_PREFIX}/lib/pkgconfig/osmesa.pc
}
